
// MultiPlayerTag_Client
//
//

//  created by PA, April 16, 2013 (derived from Processing SharedCanvasServer/Client example)
//                                    Shared Drawing Canvas (Server) by Alexander R. Galloway. 
//                                    A server that shares a drawing canvas between two computers. 
// modified by PA, April 26, 2013 (server parse and draw of muliple client messages)
// modified by PA,   May 21, 2013 (more general messaging format)

import processing.net.*;

Client c;

String input;

float data[];
boolean bIsIt = true;

float xo = 10;
float yo = 10;

String msg;

CPlayer playerMe;


void setup() 
{
  size(450, 255);
  background(204);
  stroke(0);
  frameRate(30); // Slow it down a little
  
  // Connect to the server's IP address and port
  c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port

  playerMe = new CPlayer( false );
  
  // Send my ID and initial position to the server.
//  c.write( str( playerMe.nMyID  ) + " " + str( playerMe.pos.x ) + " " + str( playerMe.pos.y ) + "\n" );
    msg = "POS";
    c.write(       msg +
             " " + str( playerMe.nMyID  ) +
             " " + str( playerMe.pos.x  ) + 
             "," + str( playerMe.pos.y  ) + "\n" );

}

void draw() 
{
  
  background(222);
  
  
 // Receive data from server.

  if ( c.available() > 0 ) {
      
    input = c.readString();
    println( input + "is what client heard" );
    
    if ( input.indexOf("\n") >= 0 ) {
        
        println( input + "is what client heard" );
        input = input.substring( 0, input.indexOf("\n") ); // Only up to the newline
        
        println( input );
    
        // Update the correct player positions.
        
        // Parse values into an array.
        //     ID pos.x pos.y
        // Parse message code and ID (header, space separated) 
        // from message body (comma separated). Alway end in /n.
        // See Jacobs language specification on teletag wiki (Google Code).
        String[] strMsgParts    = split( input, ' ' ); 
        // Parse message into array.
        String[] strMsgBody = split( strMsgParts[2], ',' ); 
        
        int nID = int( strMsgParts[1] );
        

        if ( nID != playerMe.nMyID ) {

        println( strMsgBody );
             println( str( nID ) + " is what client 2 heard" );
             xo = float( strMsgBody[0] );
             yo = float( strMsgBody[1] );
        }
        
        
        
        
        
    }
    else {
       println( "bad client message, no newline:  " + input );  
    }   
       
  }
         
  draw_other_player( xo, yo );
  
  playerMe.draw();
}

void draw_other_player( float x, float y ) {
    
        fill( 255, 255, 0 );
        ellipse( round(x), round(y), 10, 10 );    
}



void keyPressed() {
    
  if (key == CODED) {
//    if      (keyCode ==   UP)   playerMe.pos.y -= 5;
//    else if (keyCode == DOWN)   playerMe.pos.y += 5;
//    else if (keyCode == LEFT)   playerMe.pos.x -= 5;
//    else if (keyCode == RIGHT)  playerMe.pos.x += 5;

    
    switch ( keyCode ) {
        
        case    UP: playerMe.pos.y -= 5; break;
        case  DOWN: playerMe.pos.y += 5; break;
        case  LEFT: playerMe.pos.x -= 5; break;
        case RIGHT: playerMe.pos.x += 5; break;
    }
    
    // Send my ID and initial position to the server.
    msg = "POS";
    c.write(       msg +
             " " + str( playerMe.nMyID  ) +
             " " + str( playerMe.pos.x  ) + 
             "," + str( playerMe.pos.y  ) + "\n" );

  }
}

class CPlayer {

    PVector pos;
    boolean bAmIt;
    int nMyID;
    
    
    CPlayer( boolean bInitIt ){
       
        pos = new PVector( 20, 30 );
        bAmIt = bInitIt;
        nMyID = 4321;
    }
    
    void draw() {
        
        fill( 255, 0, 0 );
        ellipse( pos.x, pos.y, 10, 10 );
    }

}


